package com.kang.dead;

import com.kang.utils.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zhangkangkang
 * @Description: 死信队列
 * @date 2021/8/3 14:13
 */
public class Product {
    public static void main(String[] args) throws IOException {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();

        //创建死信交换机，设置死信交换机的路由键，交换机的类型并绑定队列
        channel.exchangeDeclare("deadexchange", "direct");
        channel.queueDeclare("deadqueue", false, false, false, null);
        channel.queueBind("deadqueue", "deadexchange", "routingkey");
        Map map = new HashMap();
        map.put("x-max-length", 10);
        map.put("x-dead-letter-exchange", "deadexchange");
        map.put("x-dead-letter-routing-key", "routingkey");
        map.put("x-overflow", "reject-publish");
//        map.put("x-overflow", "drop-head");
        channel.queueDeclare("mq1", false, false, false, map);
        for (int i = 0; i < 10; i++) {
            channel.basicPublish("", "mq1", null, new String("hello" + i).getBytes());
        }
        RabbitMQUtils.closeConnectionAndChanel(channel, connection);
    }
}
